import { Permission } from "@/api/interface/permission";
import { ResPage } from "@/api/interface/index";
import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";

/**
 * 角色管理
 */
export const getRoleList = (params: { page?: number; pageSize?: number; search?: string }) => {
  return http.post<ResPage<Permission.RoleInfo>>(PORT1 + `/admin/role/list`, params);
};

export const getAllRoles = () => {
  return http.post<Permission.RoleInfo[]>(PORT1 + `/admin/role/all`);
};

export const getRoleById = (id: string) => {
  return http.post<Permission.RoleDetail>(PORT1 + `/admin/role/detail`, { id });
};

export const createRole = (data: Permission.CreateRoleParams) => {
  return http.post(PORT1 + `/admin/role/create`, data);
};

export const updateRole = (id: string, data: Permission.UpdateRoleParams) => {
  return http.post(PORT1 + `/admin/role/update`, { id, ...data });
};

export const deleteRole = (id: string) => {
  return http.post(PORT1 + `/admin/role/delete`, { id });
};

export const getRolePermissions = (roleId: string) => {
  return http.post<Permission.RolePermissions[]>(PORT1 + `/admin/role/permissions`, { roleId });
};

export const updateRolePermissions = (params: Permission.UpdateRolePermissionsParams) => {
  return http.post(PORT1 + `/admin/role/permissions/update`, params);
};

/**
 * 权限管理
 */
export const getPermissionList = (params?: { FName?: string; FModuleId?: string }) => {
  return http.get<Permission.PermissionInfo[]>(PORT1 + `/admin/permission/list`, params);
};

export const createPermission = (data: Permission.CreatePermissionParams) => {
  return http.post(PORT1 + `/admin/permission/create`, data);
};

export const updatePermission = (id: string, data: Permission.UpdatePermissionParams) => {
  return http.post(PORT1 + `/admin/permission/update`, { id, ...data });
};

export const deletePermission = (id: string) => {
  return http.post(PORT1 + `/admin/permission/delete`, { id });
};

/**
 * 权限模块分类管理
 */
export const getPermissionModules = () => {
  return http.get<Permission.PermissionModule[]>(PORT1 + `/admin/permission/modules`);
};

export const createPermissionModule = (data: Permission.CreatePermissionModuleParams) => {
  return http.post(PORT1 + `/admin/permission/module/create`, data);
};

export const updatePermissionModule = (id: string, data: Permission.UpdatePermissionModuleParams) => {
  return http.post(PORT1 + `/admin/permission/module/update`, { id, ...data });
};

export const deletePermissionModule = (id: string) => {
  return http.post(PORT1 + `/admin/permission/module/delete`, { id });
};

export const getPermissionModuleById = (id: string) => {
  return http.post<Permission.PermissionModule>(PORT1 + `/admin/permission/module/detail`, { id });
};

/**
 * 用户角色管理
 */
export const getUserRoles = (userId: string) => {
  return http.post<Permission.UserRole[]>(PORT1 + `/admin/user/roles`, { userId });
};

export const updateUserRoles = (params: Permission.UpdateUserRolesParams) => {
  return http.post(PORT1 + `/admin/user/roles/update`, params);
};

/**
 * 角色权限管理
 * 获取当前角色的权限
 */

export const getUserCurrentPermissions = () => {
  return http.get<string[]>(PORT1 + `/admin/user/permissions/current`);
};
